import {
    selectArchivesList,
    selectArchivesDetail,
    removeArchives,
    confirmArchives,
    commitArchives,
    trialFeeArchives,
    selectFileList,
    archivesOperate,
    archivesOneTeamOperate,
    cancelArchives,
    pullArchivesByP2,
    trainInfoList,
    trainInfoDetails,
    trainInfoOperate,
    trainInfoOneTeamOperate,
    deleteTrains,
    removeOilInfo,
    oilCardList,
    calHundredConsump,
    oilInfoDetails,
    oilInfoList,
    oilBindInfoList,
    oilInfoOperate,
    oilBindInfoOperate,
    incidentalInfoList,
    pressureInfoList,
    getStartMileage,
    getEtcStartAmount,
    selectDriverAdd,
    emptyArchivesOperate,
    selectEmptyDetails,
    toLocal,
    pullIncidentals,
    getTrainNosToSZ,
    getCostAccount,
    refreshOil,
    abledStatus,
} from '@/services/fleetManage/archives';
import prompt from '@/components/Prompt';

export default {
    namespace: 'Archives',

    state: {
        archivesList: {}, // 帐单列表
        archivesDetail: {},
        formValues: {},
        oilCardList: {},
        oilInfoDetails: {},
        oilInfoList: {},
        oilBindInfoList: {},
        incidentalInfoList: {},
        trainInfoList: {},
        trainInfoDetails: {},
        pressureInfoList: {},
        feeData: {}, //费用数据
        emptyDetails: {},
        getCostAccount:{},
    },

    effects: {
        // 一、行车档案
        *abledStatus({ payload, callback }, { call }){
            const response = yield call(abledStatus, payload);
            const { code, data, message } = response;
            const content = `删除${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(response);
        },
        // 1、行车档案  列表
        *selectArchivesList({ payload, callback }, { call, put }) {
            const response = yield call(selectArchivesList, payload);
            const { code, data } = response;
            if (code !== 0) return;
            const { list, pageSize, total, pageNum } = data;
            yield put({
                type: 'saveAllValus',
                payload: {
                    archivesList: {
                        pagination: {
                            current: pageNum,
                            pageSize,
                            total,
                        },
                        list,
                    },
                },
            });
            callback && callback(list);
        },
        // 2、行车档案  详情
        *selectArchivesDetail({ payload, callback }, { call, put }) {
            const response = yield call(selectArchivesDetail, payload);
            const { code, data } = response;
            if (code !== 0) return;
            let newData = data;
            const filePayLoad = { bizId: payload.id, fileBizType: 'DRIVING' };
            const fileResponse = yield call(selectFileList, filePayLoad);
            if (fileResponse.code == 0) {
                newData.fileList = fileResponse.data;
            }
            // trainInfo
            // if (payload.isHK && data.trainInfo) {
            //   const { status, trainInfo, ...hkData } = newData;
            //   newData = { ...hkData, ...trainInfo, status }
            // }
            yield put({
                type: 'detail',
                payload: { archivesDetail: { [payload.id]: newData } },
            });
            callback && callback(newData);
        },
        // 3、行车档案  删除
        *removeArchives({ payload, callback }, { call }) {
            const response = yield call(removeArchives, payload);
            const { code, data, message } = response;
            const content = `删除${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },

        // 4、行车档案  操作：新增、编辑
        *archivesOperate({ payload, callback }, { call }) {
            const response = yield call(archivesOperate, payload);
            const { code, data, message } = response;
            const content = payload.id ? `编辑${message}` : `新增${message}`;
            if (code !== 0) return;
            if (payload.operateType == 'save') {
                prompt({ content });
            }
            callback && callback(data);
        },
        // 4、1 一车队行车档案  操作：新增、编辑
        *archivesOneTeamOperate({ payload, callback }, { call }) {
            const response = yield call(archivesOneTeamOperate, payload);
            const { code, data, message } = response;
            const content = payload.id ? `编辑${message}` : `新增${message}`;
            if (code !== 0) return;
            if (payload.operateType == 'save') {
                prompt({ content });
            }
            callback && callback(data);
        },
        // 5、行车档案  确认
        *confirmArchives({ payload, callback }, { call }) {
            const response = yield call(confirmArchives, payload);
            const { code, data, message } = response;
            const content = `确认${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        // 6、行车档案  试算
        *trialFeeArchives({ payload, callback }, { call }) {
            const response = yield call(trialFeeArchives, payload);
            const { code, data, message } = response;
            const content = `试算${message}`;
            if (code !== 0) return;
            if (payload.type == 'trial') prompt({ content });
            callback && callback(data);
            //feeData 用于存储已经试算的行车档案，当点击提交的时候，
            // 验证法1：可以通过这个判断是否已试算，如果已试算，则直接提交，否则就先试算再提交
            //验证法2：通过返回的行车档案详情， driverBillList 字段是否有值来判断
            yield put({
                type: 'detail',
                payload: { feeData: { [payload.id]: data } },
            });
        },
        // 7、行车档案  提交
        *commitArchives({ payload, callback }, { call }) {
            const response = yield call(commitArchives, payload);
            const { code, data, message } = response;
            const content = `提交${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        //8、中港车作废：
        *cancelArchives({ payload, callback }, { call }) {
            const response = yield call(cancelArchives, payload);
            const { code, data, message } = response;
            const content = `作废${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        //9、中港车重新拉取：
        *pullArchivesByP2({ payload, callback }, { call }) {
            const response = yield call(pullArchivesByP2, payload);
            const { code, data, message } = response;
            const content = `拉取${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        //10、放空档案--中港车---新增、编辑
        *emptyArchivesOperate({ payload, callback }, { call }) {
            const response = yield call(emptyArchivesOperate, payload);
            const { code, data, message } = response;
            const content = payload.id ? `编辑${message}` : `新增${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(data);
        },
        // 11、放空档案--中港车---详情
        *selectEmptyDetails({ payload, callback }, { call, put }) {
            const response = yield call(selectEmptyDetails, payload);
            const { code, data, message } = response;
            if (code !== 0) return;
            yield put({
                type: 'detail',
                payload: { emptyDetails: { [payload.id]: data } },
            });
            callback && callback(data);
        },

        // 12. 刷新油耗
        *refreshOil({payload,callback},{call,put}){
            const response=yield call(refreshOil,payload)
            const {data,code,message} = response
            if(code==0){
            prompt({content: `刷新油耗${message}!`})
              if(callback) callback(data)
            }else{
              callback&&callback(data)
            }
            
        },
        //转为本地
        *toLocal({ payload, callback }, { call }) {
            const response = yield call(toLocal, payload);
            const { code, data, message } = response;
            const content = `转为本地${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        //拉取杂费
        *pullIncidentals({ payload, callback }, { call }) {
            const response = yield call(pullIncidentals, payload);
            const { code, data, message } = response;
            const content = `拉取杂费${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        //根据费用类型获取科目
        *getCostAccount({ payload, callback }, { call, put }) {
            const response = yield call(getCostAccount, payload);
            const { code, data, message } = response;
            if (code !== 0) return;
            // yield put({
            //     type: 'detail',
            //     payload: { getCostAccount: { [payload.costType]: data } },
            // });
            callback && callback(data);
        },
        /**
         * 设置表单参数
         */
        *allValus({ payload }, { _, put }) {
            yield put({
                type: 'saveAllValus',
                payload,
            });
        },

        // 二、车次
        // 车次列表
        *trainInfoList({ payload, callback }, { call, put }) {
            const response = yield call(trainInfoList, payload);
            const { code, data } = response;
            if (code !== 0) return;
            const infoList = {
                list: data,
            };
            yield put({
                type: 'detail',
                payload: { trainInfoList: { [payload.id]: infoList } },
            });
            callback && callback(infoList);
        },
        //车次详情
        *trainInfoDetails({ payload, callback }, { call, put }) {
            const response = yield call(trainInfoDetails, payload);
            const { code, data } = response;
            if (code !== 0) return;
            let newData = data;
            const filePayLoad = { bizId: payload.id, fileBizType: 'TRAINS' };
            const fileResponse = yield call(selectFileList, filePayLoad);
            if (fileResponse.code == 0) {
                newData.fileList = fileResponse.data;
            }
            yield put({
                type: 'saveAllValus',
                payload: { trainInfoDetails: { [payload.id]: newData } },
            });
            callback && callback(newData);
        },
        *getTrainNosToSZ({ payload, callback }, { call }) {
            const response = yield call(getTrainNosToSZ, payload);
            const { code, data } = response;
            if (code !== 0) return
            callback && callback(data)
        },
        //2、车次新增、编辑
        *trainInfoOperate({ payload, callback }, { call }) {
            const response = yield call(trainInfoOperate, payload);
            const { code, data, message } = response;
            const content = payload.id ? `编辑${message}` : `新增${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(data);
        },
        //2.1 一车队 新增、编辑
        *trainInfoOneTeamOperate({ payload, callback }, { call }) {
            const response = yield call(trainInfoOneTeamOperate, payload);
            const { code, data, message } = response;
            const content = payload.id ? `编辑${message}` : `新增${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(data);
        },
        // 3、车次删除
        *deleteTrains({ payload, callback }, { call }) {
            const response = yield call(deleteTrains, payload);
            const { code, data, message } = response;
            const content = `删除${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },

        //二（一）、行车档案 --车次 -- 杂费
        //1、杂费列表
        *incidentalInfoList({ payload, callback }, { call, put }) {
            const response = yield call(incidentalInfoList, payload);
            const { code, data } = response;
            if (code !== 0) return;
            const infoList = {
                list: data,
            };
            yield put({
                type: 'detail',
                payload: { incidentalInfoList: { [payload.id]: infoList } },
            });
            callback && callback(data);
        },

        //三、 行车档案 -- 加油：     s
        //1、行车档案 -- 加油列表
        *oilInfoList({ payload, callback }, { call, put }) {
            const response = yield call(oilInfoList, payload);
            const { code, data } = response;
            if (code !== 0) return;
            // const { list, pageSize, total, pageNum } = data;
            const infoList = {
                // pagination: {
                //   current: pageNum,
                //   pageSize,
                //   total,
                // },
                list: data,
            };
            yield put({
                type: 'detail',
                payload: { oilInfoList: { [payload.id]: infoList } },
            });
            callback && callback(data);
        },
        //2、行车档案 -- 加油详情
        *oilInfoDetails({ payload, callback }, { call, put }) {
            const response = yield call(oilInfoDetails, payload);
            const { code, data } = response;
            if (code !== 0) return;
            let newData = data;
            newData.fileList = data.attachments || [];
            yield put({
                type: 'saveAllValus',
                payload: { oilInfoDetails: { [payload.id]: newData } },
            });
            callback && callback(data);
        },
        // 3、行车档案 --  新增编辑  加油记录
        *oilInfoOperate({ payload, callback }, { call }) {
            const response = yield call(oilInfoOperate, payload);
            const { code, data, message } = response;
            const content = payload.id ? `编辑${message}` : `新增${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(data);
        },

        //4、行车档案 -- 关联的加油列表
        *oilBindInfoList({ payload, callback }, { call, put }) {
            const response = yield call(oilBindInfoList, payload);
            const { code, data, message } = response;
            if (code !== 0) return;
            const { list, pageSize, total, pageNum } = data;
            const infoList = {
                pagination: {
                    current: pageNum,
                    pageSize,
                    total,
                },
                list,
            };
            yield put({
                type: 'saveAllValus',
                payload: { oilBindInfoList: infoList },
            });
            callback && callback(list);
        },

        // 5、行车档案 --  关联加油--操作
        *oilBindInfoOperate({ payload, callback }, { call }) {
            const response = yield call(oilBindInfoOperate, payload);
            const { code, message } = response;
            if (code !== 0) return;
            prompt({ content: `关联加油${message}` });
            callback && callback();
        },
        // 6、行车档案 --  移除加油记录
        *removeOilInfo({ payload, callback }, { call }) {
            const response = yield call(removeOilInfo, payload);
            const { code, data, message } = response;
            const content = `移除${message}`;
            if (code !== 0) return;
            prompt({ content });
            callback && callback(true);
        },
        //四、行车档案 --压夜
        //1、压夜列表
        *pressureInfoList({ payload, callback }, { call, put }) {
            const response = yield call(pressureInfoList, payload);
            const { code, data } = response;
            if (code !== 0) return;
            const infoList = {
                list: data,
            };
            yield put({
                type: 'detail',
                payload: { pressureInfoList: { [payload.id]: infoList } },
            });
            callback && callback(data);
        },
        // 1、油卡号
        *oilCardList({ payload }, { call, put }) {
            const response = yield call(oilCardList, payload);
            if (response.code === 0) {
                const { list, pageSize, total, pageNum } = response.data;
                yield put({
                    type: 'oilCard',
                    payload: {
                        oilCardList: {
                            pagination: {
                                current: pageNum,
                                pageSize,
                                total,
                            },
                            list,
                        },
                    },
                });
            }
        },
        // 2、计算油耗
        *calHundredConsump({ payload, callback }, { call }) {
            const response = yield call(calHundredConsump, payload);
            const { code, message, data } = response;
            if (code === 0) {
                if (callback) callback(data);
            }
        },
        *selectDriverAdd({ payload, callback }, { call }) {
            const response = yield call(selectDriverAdd, payload);
            const { code, message, data } = response;
            if (code === 0) {
                if (callback) callback(data);
            }
        },

        *getStartMileage({ payload, callback }, { call }) {
            const response = yield call(getStartMileage, payload);
            const { code, message, data } = response;
            if (code === 0) {
                if (callback) callback(data);
            }
        },
        *getEtcStartAmount({ payload, callback }, { call }) {
            const response = yield call(getEtcStartAmount, payload);
            const { code, message, data } = response;
            if (code === 0) {
                if (callback) callback(data);
            }
        },
        *selectFileList({ payload, callback }, { call }) {
            const response = yield call(selectFileList, payload);
            const { code, message, data } = response;
            if (code === 0) {
                if (callback) callback(data);
            }
        },
    },

    reducers: {
        //设置表单参数
        saveAllValus(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        oilCard(state, { payload }) {
            return {
                ...state,
                oilCardList: payload.oilCardList,
            };
        },
        detail(state, { payload }) {
            let param = '';
            for (let k in payload) {
                param = k;
            }
            return {
                ...state,
                [param]: { ...state[param], ...payload[param] },
            };
        },
    },
};
